* Table7 Summary Statistics - Treatments, Take Up, and Payouts
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if ex_post_balp==1

* Convert to actual rupee amount
replace crop_loss_amt = crop_loss_amt*10000
replace meanvill_loss = meanvill_loss*10000
replace totalpayout = totalpayout*1000
replace payout_amt = payout_amt*1000
replace payout_pp = payout_pp*1000

replace premium = premium/2 if mkt_year == 2010

gen amt_to_premium = pp_policy/premium if bought == 1

replace meanvillpayout = meanvillpayout/1000

local tablename "t7"
local sumvars "treat_year bought rebuys newbuys quitters gotpayout crop_loss"
foreach svar of varlist `sumvars' {
tabout `svar' mkt_year using "../out/`tablename'.csv", append cells(freq) format(0c) h1(Survey Sample) style(csv)
}

*Avg Policy Units
estpost sum s_policy_units if bought == 1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2006/2013 {
label var s_policy_units "s_policy_units : `i'"
estpost sum s_policy_units if mkt_year == `i' & bought == 1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

* Avg if bought, avg payout
estpost sum pp_policy payout_amt payout_pp if bought == 1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2006/2013 {
label var pp_policy "pp_policy : `i'"
label var payout_amt "payout_amt : `i'"
estpost sum pp_policy payout_amt payout_pp if mkt_year == `i' & bought == 1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

* Avg Loss 
label var pp_policy "pp_policy"
label var payout_amt "payout_amt"
label var crop_loss_amt "crop_loss_amt"
estpost sum pp_policy payout_amt payout_pp crop_loss_amt if gotpayout == 1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2008/2012 {
label var pp_policy "pp_policy : `i'"
label var payout_amt "payout_amt : `i'"
label var crop_loss_amt "crop_loss_amt : `i'"
estpost sum pp_policy payout_amt payout_pp crop_loss_amt if mkt_year == `i' & gotpayout == 1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

*Avg Premium
estpost sum premium 
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2006/2013 {
label var premium "premium : `i'"
estpost sum premium if mkt_year == `i'
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

* Avg. Payout, if received payout
estpost sum tot_gotpayout_vill meanvill_loss if meanvillpayout_pp > 0
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2008/2012 {
label var tot_gotpayout_vill "tot_gotpayout_vill : `i'"
label var meanvill_loss "meanvill_loss : `i'"
estpost sum tot_gotpayout_vill meanvill_loss if mkt_year == `i' & meanvillpayout_pp > 0
esttab using "../output/`tablename'.csv", append cells("mean(fmt(0)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

* By T1
estpost sum s_policy_units if bought == 1 & originaltvillage==1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2006/2013 {
label var s_policy_units "s_policy_units : `i'"
estpost sum s_policy_units if mkt_year == `i' & bought == 1 & originaltvillage==1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

*By T2
estpost sum s_policy_units if bought == 1 & firstt_2007==1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2007/2013 {
label var s_policy_units "s_policy_units : `i'"
estpost sum s_policy_units if mkt_year == `i' & firstt_2007==1 & bought==1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

*By TreatExp
estpost sum s_policy_units if bought == 1 & firstt_2009==1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
forvalues i = 2009/2013 {
label var s_policy_units "s_policy_units : `i'"
estpost sum s_policy_units if mkt_year == `i' & bought == 1 & firstt_2009==1
esttab using "../output/`tablename'.csv", append cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) count(fmt(0))") collabels("Mean" "Median" "S.D." "N") label  nonumbers noeqlines nogaps compress
}

estimates clear

